#!/usr/bin/env python3

# SPDX-FileCopyrightText: 2025 Qingcheng.AI
#
# SPDX-License-Identifier: Apache-2.0

# This file is used to generate a temporary requirements.txt file from
# dynamic dependencies to be resolved in setup.py. This is useful for
# build a Docker stage that only triggers rebuilds by changes of this
# file.
#
# Usage `./gen_tmp_requirements_txt.py "extra1,extra2" > requirements.txt`,
# where `extra1` and `extra2` are the extra dependency sets you want to
# include.
#
# Please note that the requirements.txt generated by this script does not
# include build time dependencies. Please install requirements-build.txt
# before running this script, or some dependencies may fail to install.

import sys

from get_requires import install_requires, extras_require

try:
    if len(sys.argv) == 1:
        extras = []
    elif len(sys.argv) == 2:
        extras = sys.argv[1].split(",") if sys.argv[1] else []
    else:
        raise ValueError("Too many arguments")
except:
    print(
        f"Usage: {sys.argv[0]} [extra1,extra2,...]",
        file=sys.stderr,
    )
    sys.exit(1)

requires = install_requires
for extra in extras:
    if extra not in extras_require:
        raise ValueError(
            f"Unknown extra '{extra}'. Available extras: {', '.join(extras_require.keys())}"
        )
    requires += extras_require[extra]

for require in requires:
    print(require)
